import React from 'react';
import {Modal, Tabs} from 'antd';
const R = require('ramda');
import TradeInfo from '../components/TradeInfo';
import DeclareInfo from '../components/DeclareInfo';
import ProductInfo from '../components/ProductInfo';
import DocumentaryInfo from '../components/DocumentaryInfo';
import ImportCD from '../components/ImportCD';
import Cost from '../components/Cost';
import Clearance from '../components/Clearance';

const TabPane = Tabs.TabPane;
export default class OrderDeclareDetail extends React.Component {

    constructor(props) {
        super(props);
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        this.state = {
            searchConditions: {},
            open: false,
            costDisabled: true,
            importCDDisabled: true,
            clearanceDisabled: true,
            attachmentDisabled: true,
            tabActiveKey: "1",
        };
    }

    handleOpen = (importCDDisabled, costDisabled, clearanceDisabled, attachmentDisabled, searchConditions, tabActiveKey) => {
        this.setState({open: true, importCDDisabled: importCDDisabled, costDisabled: costDisabled, clearanceDisabled: clearanceDisabled, attachmentDisabled: attachmentDisabled, searchConditions: searchConditions, tabActiveKey: tabActiveKey});
    }

    handleResetFields = (keepArray) => {
        if(R.isNil(keepArray)) keepArray = [];
        if(this.documentaryInfo && !R.contains("4")(keepArray))this.documentaryInfo.handleResetFields();
        if(this.importCD && !R.contains("5")(keepArray))this.importCD.handleResetFields();
        if(this.cost && !R.contains("6")(keepArray))this.cost.handleResetFields();
        if(this.clearance && !R.contains("7")(keepArray))this.clearance.handleResetFields();
    }

    handleCancel = (keepArray) => {
        this.handleResetFields(keepArray);
        this.setState({tabActiveKey: "1", open: false, searchConditions: {}}); 
    }

    handleChangeTabe = (key) => {
        this.setState({ tabActiveKey: key })
    }

    handleGetCurrencyType = () => {
        return this.declareInfo.handleGetCurrencyType();
    }

    render() {

        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;

        let widths = {"1":800, "2":800, "3": 1000, "4":800, "5": 1400, "6": 800, "7": 850};

        let title = <span>订单详情</span>
        if(dataSource.status === "3"){
            title = <span style={{color: 'red'}}>订单详情(通关退回原因: {dataSource.gobackreason})</span>
        }

        let clearance = "";
        if(R.contains(dataSource.status, ["6", "7"])){
            clearance = <TabPane tab="通关信息" key="7">{<Clearance wrappedComponentRef={(inst) => this.clearance = inst} disabled={this.state.clearanceDisabled} dataSource={this.props.dataSource} orderTgApproval = {this.props.orderTgApproval} tgManagerList={this.props.tgManagerList} dispatch={this.props.dispatch} cstId={this.props.cstId} customer={this.props.customer} searchConditions={this.state.searchConditions} handleCancel={this.handleCancel}/>}</TabPane>
        }

        return (
            <Modal maskClosable={false} footer={null} visible={this.state.open} title={title} width={widths[this.state.tabActiveKey]} wrapClassName="vertical-center-modal" onCancel={this.handleCancel.bind(this)}>
                <Tabs activeKey={this.state.tabActiveKey} tabPosition={'left'} onChange={this.handleChangeTabe.bind(this)} >
                    <TabPane tab="交易信息" key="1">{<TradeInfo wrappedComponentRef={(inst) => this.tradeInfo = inst} disabled={true} dataSource={this.props.dataSource} buyerList={this.props.buyerList} customer={this.props.customer}/>}</TabPane>
                    <TabPane tab="报关信息" key="2">{<DeclareInfo wrappedComponentRef={(inst) => this.declareInfo = inst} disabled={true} dataSource={this.props.dataSource} customer={this.props.customer}/>}</TabPane>
                    <TabPane tab="产品信息" key="3">{<ProductInfo wrappedComponentRef={(inst) => this.productInfo = inst} disabled={true} handleGetCurrencyType = {this.handleGetCurrencyType} dataSource={this.props.dataSource} drawerList={this.props.drawerList} productPage={this.props.productPage} allProducts={this.props.allproducts} dispatch={this.props.dispatch} cstId={this.props.cstId} supplyGoodsList={this.props.supplyGoodsList} customer={this.props.customer}/>}</TabPane>
                    <TabPane tab="跟单信息" key="4">{<DocumentaryInfo wrappedComponentRef={(inst) => this.documentaryInfo = inst} searchConditions={this.state.searchConditions} handleCancel={this.handleCancel} type="2" disabled={true} attachmentDisabled={this.state.attachmentDisabled} dataSource={this.props.dataSource} linkmanList={this.props.linkmanList} addressList={this.props.addressList} dispatch={this.props.dispatch} cstId={this.props.cstId} customer={this.props.customer}/>}</TabPane>

                        <TabPane tab="报关单" key="5">{<ImportCD wrappedComponentRef={(inst) => this.importCD = inst}
                                                              disabled={this.state.importCDDisabled}
                                                              dataSource={this.props.dataSource}
                                                              dispatch={this.props.dispatch} cstId={this.props.cstId}
                                                              searchConditions={this.state.searchConditions}
                                                              drawerList={this.props.drawerList}
                                                              customer={this.props.customer}
                                                              goodsAddress={this.props.goodsAddress}
                                                              linkMan={this.props.linkMan} buyer={this.props.buyer}
                                                              settlement={this.props.settlement}
                                                              tradeCountry={this.props.tradeCountry}
                                                              countryCD={this.props.countryCD}
                                                              placeClearance={this.props.placeClearance}
                                                              orderCharges={this.props.orderCharges}
                                                              supplyGoodsList={this.props.supplyGoodsList}
                                                              kindPackage={this.props.kindPackage}
                                                              supervision={this.props.supervision}
                                                              natureExemption={this.props.natureExemption}
                                                              priceItem={this.props.priceItem}
                                                              orderTgApproval={this.props.orderTgApproval}
                                                              handleCancel={this.handleCancel}
                                                              lesseeInfo={this.props.lesseeInfo}
                                                              declaration={this.props.declaration}
                                                              refreshList={this.props.refreshList}/>}
                        </TabPane>

                    <TabPane tab="费用管理" key="6" >{<Cost wrappedComponentRef={(inst) => this.cost = inst} disabled={this.state.costDisabled} dataSource={this.props.dataSource} costList={this.props.costList} costPage={this.props.costPage} dispatch={this.props.dispatch} searchConditions={this.state.searchConditions} handleCancel={this.handleCancel} type="2"/>}</TabPane>
                    {clearance}
                </Tabs>
            </Modal>
        );
    }
}